const app = getApp();
const AJAX = app.$ajax,
  $wx = app.$wx;

Page({
  data: {
    nav1: [],
    nav2: [],
    activeIndex1: -1,  
    activeIndex2: -1,
    imgHost: app.imgHost
  },

  onLoad(){
    this.getMaterialList();
  },

  onShareAppMessage(){

  },

  getStyle(e){
    let td = this.data, 
        ed = e.currentTarget.dataset, 
        style_id = ed.id,
        index = ed.index;
     
     this.setData({
       activeIndex1: td.activeIndex1 == index ? -1 : index,
       styleId: style_id
     }, setDataCallback => {
       this.getMaterialList();
     });
  },

  getAuthor(e){
    let td = this.data, 
        ed = e.currentTarget.dataset,
        author_id = ed.id,
        index = ed.index;

    this.setData({
      activeIndex2: td.activeIndex2 == index ? -1 : index,
      authorId: author_id
    }, setDataCallback => {
      this.getMaterialList();
    });
  },

  viewDetail(e){
    let ed = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/subpage/custom-made/detail/detail?detail_id=' + ed.item.goods_id
    })
  },

  /**
   * 获取素材
   * @param page
   * @param author_id
   * @parma style_id
   * */ 
  getMaterialList(){
    let td = this.data;
    AJAX.getMaterialList({
      loadType: 'bar',
      author_id: td.authorId,
      style_id: td.styleId
    }).then(([status, res, msg]) => {
      if(status){
        if(td.nav1.length == 0){
          this.setData({
            nav1: res.style_list
          });
        }
        if(td.nav2.length == 0){
          this.setData({
            nav2: res.author_list
          });
        }
        this.setData({
          material: res
        });
       
      }else{
        $wx.showToast(msg);
      }
    });
  }
});